package com.fivenations.relax;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DBAdapter {
	public static final String KEY_ROWID = "id";
	public static final String KEY_TITLE = "title";
	public static final String KEY_DATE = "duedate";
	public static final String KEY_TIME = "time";
	public static final String KEY_LOCATION = "location";
	public static final String TAG ="DbAdapter";
	
	public static final String DATABASE_NAME = "relax";
	public static final String DATABASE_TABLE = "assignments";
	public static final int DATABASE_VERSION = 1;
	
	public static final String DATABASE_CREATE =
			"create table if not exists assignments (id integer primary key autoincrement, "
			+ "title VARCHAR not null, duedate date, time time, location VARCHAR);";
	
	
	private final Context context;
	
	private DatabaseHelper DBHelper;
	private static SQLiteDatabase db;
	
	public DBAdapter(Context ctx)
	{
	     this.context = ctx;
	     DBHelper = new DatabaseHelper(context);
	}
	
	private static class DatabaseHelper extends SQLiteOpenHelper
	{
		DatabaseHelper (Context context)
		{
			super(context, DATABASE_NAME ,null, DATABASE_VERSION);
		}
		
		@Override
		public void onCreate(SQLiteDatabase db)
		{
		try { 
			db.execSQL(DATABASE_CREATE);
		} catch (SQLException e){
			e.printStackTrace();	
		  }
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			// TODO Auto-generated method stub
			Log.w(TAG, "Upgrading database from version"+ oldVersion + "to"
					+ newVersion + ", which will destroy all data");
			db.execSQL("DROP TABLE IF EXISTS events");
			onCreate(db);
		}
	}
	//=======opens the database ==========
		public DBAdapter open() throws SQLException
		{
			db = DBHelper.getWritableDatabase();
			return this;
		}
		
	//=======  closes the database   ==========
		public void close()
		{
			DBHelper.close();
		}
		
	//======  insert a record into the database  ==========
		public long insertRecord (String title, String duedate, String time, String location)
		{
			ContentValues initialValues = new ContentValues();
			initialValues.put(KEY_TITLE, title);
			initialValues.put(KEY_DATE, duedate);
			initialValues.put(KEY_TIME,time);
			initialValues.put(KEY_LOCATION, location);
			return db.insert(DATABASE_TABLE, null, initialValues);
		}
	//========   delete event   ==========
		public boolean deleteRecord(long rowid)
		{
			return db.delete(DATABASE_TABLE, KEY_ROWID, null) > 0;
		}

	//========== retrieves all records ===========
		public Cursor getAllRecords()
		{
			return db.query(DATABASE_TABLE, new String [] {KEY_ROWID, KEY_TITLE,
					KEY_DATE, KEY_TIME, KEY_LOCATION}, null, null, null, null, null);
		}
		
	//========== retrieves a particular record =================
		
		public Cursor getRecord(long rowid) throws SQLException
		{
			Cursor mCursor =
					db.query(true, DATABASE_TABLE, new String [] {KEY_ROWID, KEY_TITLE,
							KEY_DATE, KEY_TIME, KEY_LOCATION},
							KEY_ROWID + "_" + rowid, null, null, null, null, null);
			if (mCursor != null){
				mCursor.moveToFirst();
			}
			return mCursor;
		}

		//============ update record ==============
		
		public boolean updateRecord(long rowid, String title, String duedate, String time, String location){
			ContentValues args = new ContentValues();
			args.put(KEY_TITLE, title);
			args.put(KEY_DATE, duedate);
			args.put(KEY_TIME, time);
			args.put(KEY_LOCATION, location);
			return db.update(DATABASE_TABLE, args, KEY_ROWID + "_"+ rowid, null) > 0;
		}
}
